﻿using IDEAMS.core;
using IDEAMS.model;
using System.Collections.ObjectModel;
using System.Windows;

namespace IDEAMS.view.setting
{
    /// <summary>
    /// InspectionSequenceDlg.xaml 的交互逻辑
    /// </summary>
    public partial class InspectionSequenceDlg : Window
    {
        public InspectionSequenceDlgViewModel windowViewModel;
        public InspectionSequenceDlg()
        {
            InitializeComponent();
            windowViewModel = new InspectionSequenceDlgViewModel();
            this.DataContext = windowViewModel;

            this.btnAll.Click += new RoutedEventHandler(All_Click);
            this.btnNone.Click += new RoutedEventHandler(None_Click);
            this.btnLeft.Click += new RoutedEventHandler(Left_Click);
            this.btnRight.Click += new RoutedEventHandler(Right_Click);
            this.btnUp.Click += new RoutedEventHandler(Up_Click);
            this.btnDown.Click += new RoutedEventHandler(Down_Click);

            this.btnOK.Click += new RoutedEventHandler(OK_Click);
            this.btnCancel.Click += new RoutedEventHandler(Cancel_Click);
        }

        private void All_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in windowViewModel.items)
            {
                windowViewModel.inspectionSequences.Add(item);
            }
            windowViewModel.items.Clear();
        }

        private void None_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in windowViewModel.inspectionSequences)
            {
                windowViewModel.items.Add(item);
            }
            windowViewModel.inspectionSequences.Clear();
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            TW_App.globalSetting.inspectionSequences.Clear();
            foreach (var item in windowViewModel.inspectionSequences)
            {
                TW_App.globalSetting.inspectionSequences.Add((int)item);
            }
            TW_App.globalSetting.Save();

            this.DialogResult = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Left_Click(object sender, RoutedEventArgs e)
        {
            if (this.lbItem2.SelectedItems.Count != 1) return;

            ProcessStep item = (ProcessStep)this.lbItem2.SelectedItems[0];

            windowViewModel.inspectionSequences.Remove(item);
            windowViewModel.items.Add(item);
            
        }

        private void Right_Click(object sender, RoutedEventArgs e)
        {
            if (this.lbItem1.SelectedItems.Count != 1) return;

            ProcessStep item = (ProcessStep)this.lbItem1.SelectedItems[0];

            windowViewModel.inspectionSequences.Add(item);
            windowViewModel.items.Remove(item);
        }

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            if (this.lbItem2.SelectedItems.Count != 1) return;

            ProcessStep item = (ProcessStep)this.lbItem2.SelectedItems[0];

            int index = windowViewModel.inspectionSequences.IndexOf(item);
            if (index == 0) return;
            windowViewModel.inspectionSequences.Remove(item);
            windowViewModel.inspectionSequences.Insert(index - 1, item);

            this.lbItem2.SelectedIndex = index - 1;
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            if (this.lbItem2.SelectedItems.Count != 1) return;

            ProcessStep item = (ProcessStep)this.lbItem2.SelectedItems[0];

            int index = windowViewModel.inspectionSequences.IndexOf(item);
            if (index >=windowViewModel.inspectionSequences.Count-1) return;
            windowViewModel.inspectionSequences.Remove(item);
            windowViewModel.inspectionSequences.Insert(index + 1, item);
            

            this.lbItem2.SelectedIndex = index + 1;
        }
    }

    public class InspectionSequenceDlgViewModel : ViewModelBase
    {
        public InspectionSequenceDlgViewModel()
        {
            items = new ObservableCollection<ProcessStep>();
            items.Add(ProcessStep.PRE_DATA_ACQUISITION);
            items.Add(ProcessStep.PRE_ETALON);
            items.Add(ProcessStep.PRE_SPECTRUM_GRATING);
            items.Add(ProcessStep.PRE_SPECTRUM_NO_GRATING);
            items.Add(ProcessStep.CONTINUOUS_DETECTION);
            items.Add(ProcessStep.POST_SPECTRUM_NO_GRATING);
            items.Add(ProcessStep.POST_SPECTRUM_GRATING);
            items.Add(ProcessStep.POST_ETALON);
            items.Add(ProcessStep.POST_DATA_ACQUISITION);
            inspectionSequences = new ObservableCollection<ProcessStep>();

            
            foreach (var item in TW_App.globalSetting.inspectionSequences)
            {
                inspectionSequences.Add((ProcessStep)item);
                items.Remove((ProcessStep)item);
            }
        }


        private ObservableCollection<ProcessStep> m_items;
        public ObservableCollection<ProcessStep> items
        {
            get { return m_items; }
            set
            {
                m_items = value;
                NotifyPropertyChanged();
            }
        }

        private ObservableCollection<ProcessStep> m_inspectionSequences;
        public ObservableCollection<ProcessStep> inspectionSequences
        {
            get { return m_inspectionSequences; }
            set
            {
                m_inspectionSequences = value;
                NotifyPropertyChanged();
            }
        }
    }
}
